package com.caikun.model.common;

import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = -3720998571176536865L;

    /**
     * 数据条数
     */
    private List<T> records = new ArrayList<>();

    /**
     * 一共有多少条数据
     */
    private long totalElements;

    /**
     * 当前页数
     */
    private long currentPage;

    /**
     * 一页数据最多有多少条数据
     */
    private long pageSize;

    /**
     * 一共有多少页
     */
    private long totalPages;

    /**
     * 当前页数有多少条数据
     */
    private int numberOfElements;

    public PageResult(List<T> records, long totalElements, long currentPage, long pageSize, long totalPages, int numberOfElements) {
        this.records = records;
        this.totalElements = totalElements;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalPages = totalPages;
        this.numberOfElements = numberOfElements;
    }


    public PageResult(PageInfo<T> pageInfo) {
        this.records = pageInfo.getList();
        this.totalElements = pageInfo.getTotal();
        this.currentPage = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.totalPages = pageInfo.getPages();
        this.numberOfElements = pageInfo.getSize();
    }



    public static <T> PageResult<T> of(PageInfo<T> pageInfo) {
        return new PageResult<>(pageInfo);
    }

    public static <T> PageResult<T> of(List<T> list, PageInfo<?> pageInfo) {
        return new PageResult<>(list,
                pageInfo.getTotal(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getPages(),
                list.size());
    }

}
